﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Comm.Model
{
   public class common
    {
        /// <summary>
        /// 对时间差进行处理（DateTime类型相减得到的结果类型是TimeSpan）
        /// 3天5小时20分钟   time.TotalHours:   3*24+5+20/60
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetTimeSpan(TimeSpan time)
        {
            if (time.TotalDays >= 365)
            {
                return Math.Floor(time.TotalDays / 365) + "年前";
            }
            else if (time.TotalDays >= 30)
            {
                return Math.Floor(time.TotalDays / 30) + "月前";

            }
            else if (time.TotalHours >= 24)
            {
                return Math.Floor(time.TotalDays) + "天前";
            }
            else if (time.TotalHours >= 1)
            {
                return Math.Floor(time.TotalHours) + "小时前";
            }
            else if (time.TotalMinutes >= 1)
            {
                return Math.Floor(time.TotalMinutes) + "分钟前";
            }
            else
            {
                return "刚刚";
            }
        }

        /// <summary>
        /// 将UBB代码转换为HTML代码。
        /// </summary>
        /// <param name="ubb"></param>
        /// <returns></returns>
        public static string Ubb2Html(string ubb)
        {
            //[url=http://www.bookshop.com]图书商城[/url]
            //[color=#FFFFFF]颜色[/color]
            //[b]aaa[/b]
            string html;

            html = Regex.Replace(ubb, @"\[url=(.+?)\](.+?)\[/url\]", "<a href='$1'>$2</a>");
            html = Regex.Replace(html, @"\[color=(.+?)\](.+?)\[/color\]", "<font color='$1'>$2</font>");
            html = Regex.Replace(html, @"\[b\](.+?)\[/b\]", "<b>$1</b>");



            return html;
        }

        /// <summary>
        /// 分页数据实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class PagedAjaxData<T>
        {
            /// <summary>
            /// 总页数
            /// </summary>
            public int PageCount;

            /// <summary>
            /// 总行数
            /// </summary>
            public int RowCount;

            /// <summary>
            /// 当前页码
            /// </summary>
            public int PageIndex;

            /// <summary>
            /// 当前页码数据
            /// </summary>
            public IList<T> PagedList;

            /// <summary>
            /// 当前出版社数据
            /// </summary>
            public IList<Model.Publishers> PublishList;
        }

    }
}
